﻿using Com.Lancetinc.Evs.Helpers;
using Com.Lancetinc.Evs.Reports;
using Prism.Commands;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Windows;
using GongSolutions.Wpf.DragDrop;
using Com.Lancetinc.Evs.Data.Domain;
using Com.Lancetinc.Evs.Data;

namespace Com.Lancetinc.Evs.ViewModels
{
    public class ReportViewModel : ViewModelBase, IDropTarget
    {
        public DelegateCommand<string> DeleteImageCommand { get; }

        public ReportViewModel(ExperViewModel experVm)
        {
            ExperVm = experVm;
            foreach (var reportImage in ReportHelper.ParseReportImages(experVm.Exper.SeqNo, experVm.Exper.ReportImages))
            {
                Images.Add(reportImage);
            }

            DeleteImageCommand = new DelegateCommand<string>(ExecuteDeleteImageCommand);
        }

        private void ExecuteDeleteImageCommand(string img)
        {
            Images.Remove(img);
            SaveReportImages();
        }

        /// <summary>
        /// 
        /// </summary>
        public ExperViewModel ExperVm { get; }

        /// <summary>
        /// 插图,全路径
        /// </summary>
        public IList<string> Images { get; } = new ObservableCollection<string>();

        /// <summary>
        /// 检查时间
        /// </summary>
        public string InspectionTime
        {
            get
            {
                if (string.IsNullOrEmpty(ExperVm.StartTime))
                    return string.Empty;
                var startTime = DateUtil.ToDate(ExperVm.StartTime);
                if (!startTime.HasValue)
                    return string.Empty;
                return startTime.Value.ToString("yyyy-MM-dd");
            }
        }

        /// <summary>
        /// 保存报告图片到数据库
        /// </summary>
        public void SaveReportImages()
        {
            var imageNames = Images.Select(Path.GetFileName).ToArray();
            var images = string.Join("\n", imageNames);
            ExperVm.Exper.ReportImages = images;
            AppRepositories.Exper.UpdatePartial(ExperVm.Exper, nameof(Exper.ReportImages));
        }

        #region Drag/Drop

        private readonly ReportDropHandler _dropHandler = new();
        /// <inheritdoc />
        void IDropTarget.DragEnter(IDropInfo dropInfo)
        {
        }

        void IDropTarget.DragOver(IDropInfo dropInfo)
        {
            if (dropInfo.Data is ImageViewModel imgVm)
            {
                if (Images.Count >= AppConsts.ReportMaxImages)
                    return; // 超过最大数量，禁止添加

                if (Images.Contains(imgVm.ImageFullPath))
                    return;

                dropInfo.Effects = DragDropEffects.Move;
                dropInfo.DropTargetAdorner = DropTargetAdorners.Insert;
                return;
            }
            _dropHandler.DragOver(dropInfo);
        }

        /// <inheritdoc />
        void IDropTarget.DragLeave(IDropInfo dropInfo)
        {
        }

        void IDropTarget.Drop(IDropInfo dropInfo)
        {
            if (dropInfo?.DragInfo == null)
            {
                return;
            }

            if (dropInfo.Data is ImageViewModel imgVm)
            {
                if (Images.Count >= AppConsts.ReportMaxImages)
                    return; // 超过最大数量，禁止添加

                if (Images.Contains(imgVm.ImageFullPath))
                    return;

                var insertIndex = ReportDropHandler.GetInsertIndex(dropInfo);
                Images.Insert(insertIndex, imgVm.ImageFullPath);
                SaveReportImages();
                return;
            }
            _dropHandler.Drop(dropInfo);
            SaveReportImages();
        }
        #endregion
    }
}